import { createApp } from 'vue'
import Loading from './Loading.vue'

interface LoadingOptions {
  duration?: number;
  mask?: boolean;
}

const loading= {
  instance: null as any,
  parent: document.createElement('div'),
  showLoading({  duration = 5000, mask = false }: LoadingOptions) {
    if (this.instance !== null) this.clear() 
    this.instance = createApp(Loading, { mask })
    document.body.appendChild(this.parent)
    this.instance.mount(this.parent)
    setTimeout(() => {
      this.clear()
    }, duration)
  },
  hideLoading() {
    this.clear()
  },
  clear() {
    if (this.instance !== null) {
      this.instance.unmount(this.parent)
      document.body.removeChild(this.parent)
      this.instance = null
    }
  },
}

export default loading
